package com.gestionabsences.servlets;

import com.gestionabsences.beans.WebUser;
import com.gestionabsences.config.DAOFactory;
import com.gestionabsences.form.FormConnection;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author matt
 */
//@WebServlet(name = "ConnectionUser", urlPatterns = {"/connection"}, loadOnStartup=1)
public class ConnectionUser extends HttpServlet {

    private static final String ATT_DAO_FACTORY = "daofactory";
    private DAOFactory dao;
    
    public static final String ATT_WEBUSER = "webuser";
    public static final String ATT_FORM   = "form";
    public static final String ATT_SESSION_WEBUSER = "sessionWebuser";

    public static final String VUE_SUCCES_STUDENT = "restricted/Students/home";
    public static final String VUE_SUCCES_TEACHER = "restricted/Teacher/home";
    public static final String VUE_SUCCES_SECRETARY = "restricted/Secretary/home";
    public static final String VUE_SUCCES_ADMIN = "restricted/Admin/home";
    
    public static final String VUE_FORM = "/WEB-INF/connection.jsp";
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	this.getServletContext().getRequestDispatcher( VUE_FORM ).forward( request, response );
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
        
	/* Récupération de la session depuis la requête */
        HttpSession session = request.getSession();
	
	FormConnection form = new FormConnection(dao);

        /* Traitement de la requête et récupération du bean en résultant */
        WebUser user = form.creerWebUser( request );

        /* Ajout du bean et de l'objet métier à l'objet requête */
        request.setAttribute( ATT_WEBUSER, user );
        request.setAttribute( ATT_FORM, form );

        if ( form.getErreurs().isEmpty() ) {
	    session.setAttribute( ATT_SESSION_WEBUSER, user );
            /* Si aucune erreur, alors affichage de la fiche récapitulative */
            //this.getServletContext().getRequestDispatcher( VUE_SUCCES ).forward( request, response );
	    
	    if(user.getType() == WebUser.TypeUser.STRING_ONE) {
		response.sendRedirect( VUE_SUCCES_STUDENT );
	    } else if(user.getType() == WebUser.TypeUser.STRING_TWO) {
		response.sendRedirect( VUE_SUCCES_TEACHER );
	    } else if(user.getType() == WebUser.TypeUser.STRING_THREE) {
		response.sendRedirect( VUE_SUCCES_SECRETARY );
	    } else {
		response.sendRedirect( VUE_SUCCES_ADMIN );
	    }
		
        } else {
	    session.setAttribute( ATT_SESSION_WEBUSER, null );
            /* Sinon, ré-affichage du formulaire de création avec les erreurs */
            this.getServletContext().getRequestDispatcher( VUE_FORM ).forward( request, response );
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
	return "Servlet pour la  connection des utilisateurs";
    }// </editor-fold>
    
    @Override
    public void init() throws ServletException {
	dao = (DAOFactory) getServletContext().getAttribute(ATT_DAO_FACTORY);
    }
}
